<!--
 * @Author: liu
 * @Date: 2024-05-13 01:35:32
 * @LastEditors: liu
 * @LastEditTime: 2024-05-18 00:20:39
 * @FilePath: \yyx_admin\src\views\courses\components\ChapterStatusChange.vue
 * @Description: 
 * 天道酬勤
-->
<template>
  <div>
    <el-dialog
      v-model="dialogFormVisible"
      title="更改章节状态"
      width="500"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <el-result icon="info" :title="title" />
      <el-form :model="form">
        <el-form-item label="状态更改为：" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="Please select a zone">
            <el-option
              v-for="(item, index) in chapterStatusText"
              :key="index"
              :label="item"
              :value="index"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button style="color: #606266" @click="dialogFormVisible = false"
            >取消</el-button
          >
          <el-button type="primary" @click="onSubmit"> 确认 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { updateChapterStatus } from '@/api/course/index'
import { ElMessage } from 'element-plus'

const dialogFormVisible = ref(false)
const formLabelWidth = '100px'

const form = reactive({
  status: 0
})

let title = ''
//章节状态文字
const chapterStatusText = ['隐藏', '待更新', '已发布']

//声明事件消息
const emits = defineEmits<{
  (e: 'statusChange'): void
}>()

//对话框初始化
const showAndInit = (chapterInfo: any) => {
  // console.log(courseInfo, chapterInfo)
  title = '当前状态为：' + chapterStatusText[chapterInfo.status]
  Object.assign(form, chapterInfo)
  dialogFormVisible.value = true
}

const onSubmit = async () => {
  const res = await updateChapterStatus(form)
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: `更新章节状态成功！`
    })
    dialogFormVisible.value = false
    emits('statusChange')
  } else {
    ElMessage({
      type: 'error',
      message: `更新章节状态失败！`
    })
    throw new Error('更新章节状态失败')
  }
}
defineExpose({
  showAndInit
})
</script>

<style scoped></style>
